package com.example.springboottestgit.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.example.springboottestgit.bean.BusinessException;
import com.example.springboottestgit.bean.vo.InputVO;
import com.example.springboottestgit.bean.vo.OutputVO;
import com.example.springboottestgit.enums.ExceptionCodeEnum;
import lombok.extern.slf4j.Slf4j;

/**
 * @description:
 * @author: ZZZ
 */
@Slf4j
public class HttpUtils {
    public static String post(String url, String param) {
        String result = cn.hutool.http.HttpUtil.createPost(url)
                //这个请求头.header是自己项目需要加的，可以省略
                .header("Content-Type", "application/json")
                //这两个请求头是项目需要加的，可以省略
                //传输参数
                .body(param).execute().body();

        return result;
    }


    public static String tenderUnite(String question, String extraParam, String type, String url) {
        InputVO vo = new InputVO();
        vo.setQuestion(question);
        vo.setExtra_param(extraParam);
        vo.setType(type);
        String param = JSONObject.toJSONString(vo);
        log.info("招投标统一接口参数：" + param);
        String result = null;
        try {
            String resultData = HttpUtil.createPost(url).header("Content-Type", "application/json").body(param).execute().body();
            //String resultData = "{\"code\":\"000000\",\"msg\":\"success\",\"data\":{\"result\":\"符合\"}}";
            //log.info("招投标检索结果：" + result);

            OutputVO outputVO = JSONObject.parseObject(resultData, OutputVO.class);
            if ("000000".equals(outputVO.getCode())) {
                result = outputVO.getData().getResult();
            } else {
                log.error("招投标检索接口调用失败,返回结果：{}", resultData);
                log.error("招投标检索接口调用失败,接口参数：{}", param);
                throw new BusinessException(ExceptionCodeEnum.INTERFACE_CALL_FAIL.code, "招投标统一接口调用失败");
            }

        } catch (Exception e) {
            log.error("招投标检索接口调用失败", e);
            log.error("招投标检索接口调用失败,接口参数：{}", param);
            throw new BusinessException(ExceptionCodeEnum.INTERFACE_CALL_FAIL.code, "招投标统一接口调用失败");
        }
        return result;

    }

    //问题检索接口
    public static String tenderQuestionRetrieval(String question, String fragment, String url) {
        InputVO vo = new InputVO();
        vo.setQuestion(question);
        vo.setFragment(fragment);
        String param = JSONObject.toJSONString(vo);
        log.info("招投标检索参数：" + param);
        String result = null;
        try {
            result = HttpUtil.createPost(url).header("Content-Type", "application/json").body(param).execute().body();
            //result = "{\"result\":\"符合,符合招投文件要求\",\"fragment\":\"片段测试\"}";
            //log.info("招投标检索结果：" + result);
        } catch (Exception e) {
            log.error("招投标检索接口调用失败", e);
            throw new BusinessException(ExceptionCodeEnum.INTERFACE_CALL_FAIL.code, "招投标检索接口调用失败");
        }
        if ("Internal Server Error".equals(result)) {
            log.error("招投标检索接口调用失败 Internal Server Error");
            throw new BusinessException(ExceptionCodeEnum.INTERFACE_CALL_FAIL.code, "招投标检索接口调用失败");
        }
        return result;
    }


    //匹配接口
    public static String tenderMateResult(String inviteTenderResult, String submitTenderResult, String url) {
        InputVO vo = new InputVO();
        vo.setInviteTender(inviteTenderResult);
        vo.setSubmitTender(submitTenderResult);
        String param = JSONObject.toJSONString(vo);
        log.info("招投标匹配参数：" + param);
        String result = null;
        try {
            result = HttpUtil.createPost(url).header("Content-Type", "application/json").body(param).execute().body();
            //result = "{\"result\":\"符合，招投标匹配成功\",\"fragment\":\"123\"}";
            //result = getData(result);
            //log.info("招投标匹配结果：" + result);
        } catch (Exception e) {
            log.error("招投标匹配接口调用失败", e);
            throw new BusinessException(ExceptionCodeEnum.INTERFACE_CALL_FAIL.code, "招投标匹配接口调用失败");
        }
        if ("Internal Server Error".equals(result)) {
            log.error("招投标匹配接口调用失败 Internal Server Error");
            throw new BusinessException(ExceptionCodeEnum.INTERFACE_CALL_FAIL.code, "招投标匹配接口调用失败");
        }
        return result;
    }

    //法律法规校验接口
    public static String lawCheck(String content, String url) {
        InputVO vo = new InputVO();
        vo.setLaw(content);
        String param = JSONObject.toJSONString(vo);
        log.info("法律法规合法性校验参数：" + param);
        String result = null;
        try {
            result = HttpUtil.createPost(url).header("Content-Type", "application/json").body(param).execute().body();
            //result = "{\"result\":\"符合\",\"fragment\":\"123\"}";
            //result = getData(result);
            //log.info("招投标匹配结果：" + result);
        } catch (Exception e) {
            log.error("法律法规合法性校验接口调用失败", e);
            throw new BusinessException(ExceptionCodeEnum.INTERFACE_CALL_FAIL.code, "法律法规合法性校验接口调用失败");
        }
        if ("Internal Server Error".equals(result)) {
            log.error("法律法规合法性校验接口调用失败 Internal Server Error");
            throw new BusinessException(ExceptionCodeEnum.INTERFACE_CALL_FAIL.code, "法律法规合法性校验接口调用失败");
        }
        return result;

    }

    //授权委托书 校验接口
    public static String proveCheck(String prove, String name, String url) {
        InputVO vo = new InputVO();
//        vo.setProjectNo(projectNo);
//        vo.setCompanyName(companyName);
//        vo.setLegalRepresentative(legalRepresentative);
//        vo.setDelegator(delegator);
        vo.setProve(prove);
        vo.setName(name);

        String param = JSONObject.toJSONString(vo);
        log.info("授权委托书合法性校验参数：" + param);
        String result = null;
        try {
            result = HttpUtil.createPost(url).header("Content-Type", "application/json").body(param).execute().body();
            //result = "{\"result\":\"符合\",\"fragment\":\"123\"}";
            //result = getData(result);
            //log.info("招投标匹配结果：" + result);
        } catch (Exception e) {
            log.error("授权委托书合法性校验接口调用失败", e);
            throw new BusinessException(ExceptionCodeEnum.INTERFACE_CALL_FAIL.code, "授权委托书合法性校验接口调用失败");
        }
        if ("Internal Server Error".equals(result)) {
            log.error("授权委托书合法性校验接口调用失败 Internal Server Error");
            throw new BusinessException(ExceptionCodeEnum.INTERFACE_CALL_FAIL.code, "授权委托书合法性校验接口调用失败");
        }
        return result;

    }

    public static String submitTenderTechnologyQuery(String param, String url) {

        log.info("投标技术要求查询 参数：" + param);
        String result = null;
        try {
            result = HttpUtil.createPost(url).header("Content-Type", "application/json").body(param).execute().body();
            //result = "{\"submitTender\": \"投标技术要求测试\",\"mateResult\": \"不符合\"}";
            //result = getData(result);
            //log.info("招投标匹配结果：" + result);
        } catch (Exception e) {
            log.error("投标技术要求查询 接口调用失败", e);
            throw new BusinessException(ExceptionCodeEnum.INTERFACE_CALL_FAIL.code, "投标技术要求查询 接口调用失败");
        }
        return result;

    }


    public static String tenderFamiliarityMate(String inviteTender, String submitTender, int segmentType, String url) {
        InputVO vo = new InputVO();
        vo.setInviteTender(inviteTender);
        vo.setSubmitTender(submitTender);
        vo.setSegmentType(segmentType);
        String param = JSONObject.toJSONString(vo);
        log.info("技术要求相识度匹配 参数：" + param);
        String result = null;
        try {
            result = HttpUtil.createPost(url).header("Content-Type", "application/json").body(param).execute().body();
            //result = "{\"submitTender\": \"投标技术要求测试\",\"result\": [\"不符合\"]}";
            //result = getData(result);
            //log.info("招投标匹配结果：" + result);
        } catch (Exception e) {
            log.error("招投标技术要求 相识度匹配 接口调用失败", e);
            throw new BusinessException(ExceptionCodeEnum.INTERFACE_CALL_FAIL.code, "招投标技术要求 相识度匹配 接口调用失败");
        }
        return result;

    }


    //负面清单校验接口
    public static String negativeListCheck(String policyBasis, String fragment, String url) {
        InputVO vo = new InputVO();
        vo.setPolicyBasis(policyBasis);
        vo.setFragment(fragment);
        //vo.setFile_path(filePath);
        //vo.setSearch(searchContent);
        //vo.setIndexId(indexId);
        String param = JSONObject.toJSONString(vo);
        log.info("负面清单校验接口参数：" + param);
        String result = null;
        try {
            result = HttpUtil.createPost(url).header("Content-Type", "application/json").body(param).execute().body();
            //result = "{\"result\":\"符合,符合招投文件要求\",\"fragment\":\"片段测试\"}";
            //log.info("招投标检索结果：" + result);
        } catch (Exception e) {
            log.error("负面清单校验接口调用失败", e);
            throw new BusinessException(ExceptionCodeEnum.INTERFACE_CALL_FAIL.code, "负面清单校验接口调用失败");
        }
        if ("Internal Server Error".equals(result)) {
            log.error("负面清单校验接口调用失败 Internal Server Error");
            throw new BusinessException(ExceptionCodeEnum.INTERFACE_CALL_FAIL.code, "负面清单校验接口调用失败");
        }
        return result;
    }


    public static String getData(String answer) {
        if (answer.endsWith("\"")) {
            answer = answer.substring(0, answer.length() - 1);
        }

        if (answer.startsWith("\"")) {
            answer = answer.substring(1);

        }
        answer = answer.replace("\\n\\n", "\n");
        answer = answer.replace("\\n", "\n");
        return answer;
    }


}
